/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.ui.nature;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.zoundry.gaedt.core.util.ZGaeUtil;


/**
 * Util for dealing with natures.
 */
public class ZGaeNatureUtil
{
    /**
     * Returns true if the given project has the Orchestration nature installed.
     * 
     * @param aProject
     */
    public static boolean hasNature(IProject aProject)
    {
        try
        {
            IProjectDescription description = aProject.getDescription();
            String[] natures = description.getNatureIds();

            for (int i = 0; i < natures.length; ++i)
            {
                if (ZGaeNature.NATURE_ID.equals(natures[i]))
                {
                    return true;
                }
            }
        }
        catch (CoreException e)
        {
            ZGaeUtil.logError(e);
        }
        return false;
    }

    /**
     * Adds the orchestration nature to the given project.
     * 
     * @param aProject
     */
    public static void addNature(IProject aProject)
    {
        try
        {
            IProjectDescription description = aProject.getDescription();
            String[] natures = description.getNatureIds();
            String[] newNatures = new String[natures.length + 1];
            System.arraycopy(natures, 0, newNatures, 1, natures.length);
            newNatures[0] = ZGaeNature.NATURE_ID;
            description.setNatureIds(newNatures);
            aProject.setDescription(description, null);
        }
        catch (CoreException e)
        {
            ZGaeUtil.logError(e);
        }
    }

    /**
     * Adds the passed builder to the project.
     * 
     * @param aBuilderId
     * @throws CoreException
     */
    public static void addBuilder(String aBuilderId, IProject aProject) throws CoreException
    {
       IProjectDescription desc = aProject.getDescription();
       ICommand[] commands = desc.getBuildSpec();

       for (int i = 0; i < commands.length; ++i)
       {
          if ( commands[i].getBuilderName().equals( aBuilderId ) )
          {
             return;
          }
       }

       ICommand[] newCommands = new ICommand[commands.length + 1];
       System.arraycopy(commands, 0, newCommands, 0, commands.length);
       ICommand command = desc.newCommand();
       command.setBuilderName( aBuilderId );
       newCommands[newCommands.length - 1] = command;
       desc.setBuildSpec(newCommands);
       aProject.setDescription(desc, null);
    }

    /**
     * Removes the passed builder from the project.
     * 
     * @param aBuilderId
     * @throws CoreException
     */
    public static void removeBuilder(String aBuilderId, IProject aProject) throws CoreException
    {
       IProjectDescription description = aProject.getDescription();
       ICommand[] commands = description.getBuildSpec();
       for (int i = 0; i < commands.length; ++i)
       {
          if ( commands[i].getBuilderName().equals(aBuilderId) )
          {
             ICommand[] newCommands = new ICommand[commands.length - 1];
             System.arraycopy(commands, 0, newCommands, 0, i);
             System.arraycopy(commands, i + 1, newCommands, i, commands.length - i - 1);
             description.setBuildSpec(newCommands);
             aProject.setDescription(description, null);
             break;
          }
       }
    }
}
